package view.monster
{
	import data.Globals;
	import data.MonsterProp;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import manager.EffectName;
	import manager.MapConfig;
	import manager.ObjDataManage;
	import manager.ObjectPool;
	
	import utils.GameUtils;
	
	public class BaseMonster extends Sprite
	{
		protected var cls:Class;
		
		public var speed:Number;
		
		private var _blood:int;
		
		private var xSpeed:Number;
		
		private var ySpeed:Number;
		
		private var line:Array;
		
		private var cur:Point;//当前点
		
		private var next:Point;//下一点
		
		private var three:Point;//第三点 以确定当前点是否需要拐弯
		
		private var bloodBar:BloodBarCls;
		
		public var onRemove:Function;
		
		private var _hurt:int;
		
		/**
		 *需要旋转角度 
		 */
		private var needRotateValue:int;
		
		private var threeCopy:Point;
		
		/**
		 *已经旋转角度 
		 */
		private var haveRotate:int;
		
		private var _exp_rad:int;
		
		private var _energy:int;
		
		public function BaseMonster()
		{
			super();
			var bm:Bitmap = new cls();
			this.addChild(bm);
			bm.width = Globals.GRAD_WIDTH;
			bm.height = Globals.GRAD_HEIGHT;
			bm.x = -bm.width/2;
			bm.y = -bm.height/2;
			line=[];
			bloodBar = new BloodBarCls();
			bloodBar.rotation = 90;
			bloodBar.mouseChildren = false;
			bloodBar.mouseEnabled = false;
			bloodBar.x = -10;
			this.addChild(bloodBar);
			bloodBar.stop();
			this.buttonMode = true;
		}
		
		public function move():void{
			var turn:Boolean = checkNeedTurn();
			if(needRotateValue != 0){
				var factor:int = needRotateValue/Math.abs(needRotateValue);//正负号系数
				var value:int =factor*(speed+1);
				haveRotate+=value;
				if(Math.abs(haveRotate)<=90){
					this.rotation+=value;
					if(Math.abs(haveRotate)==90){
						needRotateValue = 0;
						haveRotate = 0;
					}
				}else{
					this.rotation-=(90-(Math.abs(haveRotate)-Math.abs(value)))*factor;
					needRotateValue = 0;
					haveRotate = 0;
				}
			}
			this.x = this.x + this.xSpeed;
			this.y = this.y + this.ySpeed;
			checkIsOutBorder();
		}
		/**
		 *超出屏幕需要从场景内移除 并放入内存池 
		 * 
		 */		
		private function checkIsOutBorder():void
		{
			if(this.x<0||this.x>Globals.stage_width||this.y<0||this.y>Globals.stage_height){
				if(this.x>Globals.stage_width){
					Globals.towerWar.throughFail();
					return;
				}
				die();
			}
		}
		
		/**
		 *死亡 
		 * 
		 */
		public function die():void
		{
			if(this.parent){
				this.parent.removeChild(this);
				onRemove(this);
				ObjectPool.putObj(this);
			}
		}
		
		public function resetData():void{
			this.rotation = 0;
			this.needRotateValue = 0;
			this.haveRotate = 0;
			this.threeCopy = null;
			MapConfig.getInstance().copeline(line);
			cur = line.shift();
			next =  line.shift();
			three = line.shift();
			upSpeed();
		}
		/**
		 *是否到了下一个节点 需要转弯 
		 */
		private function checkNeedTurn():Boolean
		{
			var nextSpeed:Point = GameUtils.getSpeed(next,three,speed);
			
			if(int(this.xSpeed)!=0){
				if(int(nextSpeed.x)==0&&needRotateValue==0){
					if(this.xSpeed>0){
						if(threeCopy != three)
							nextSpeed.y>0 ? needRotateValue = 90 : needRotateValue = -90;
					}else{
						if(threeCopy != three)
						nextSpeed.y>0 ? needRotateValue = -90 : needRotateValue = 90;
					}
				}
				if(Math.abs(next.x - this.x)<Math.abs(this.xSpeed)){
					if(line.length){
						cur = next;
						next = three;
						three = line.shift();
						upSpeed();
						return true;
					}
				}
			}else{
				if(int(nextSpeed.y)==0&&needRotateValue == 0){
					if(this.ySpeed>0){
						if(threeCopy != three)
						nextSpeed.x>0 ? needRotateValue = -90 : needRotateValue = 90;
					}else{
						if(threeCopy != three)
						nextSpeed.x>0 ? needRotateValue = 90 : needRotateValue = -90;
					}
				}
				if(Math.abs(next.y - this.y)<Math.abs(this.ySpeed)){
					if(line.length){
						cur = next;
						next = three;
						three  =line.shift();
						upSpeed();
						return true;
					}
				}
			}
			
			if(needRotateValue){
				threeCopy = three;
			}
			return false;
		}
		
		private function upSpeed():void{
			var s:Point = GameUtils.getSpeed(cur,next,speed);
			this.x = cur.x;
			this.y = cur.y;
			xSpeed = s.x;
			ySpeed = s.y;
		}
		
		public function set blood(value:int):void
		{
			_blood = value;
			var mp:MonsterProp = ObjDataManage.getInstance().getMonsterProp(this.name);
			bloodBar.gotoAndStop(int((mp.blood-value)/mp.blood*bloodBar.totalFrames));
			if(value<=0){
				Globals.gameView.calculateScore(mp.energy);
				Globals.gameView.createEnergyMotion(mp.energy,this.x,this.y);
				Globals.gameView.createExplode(EffectName.A_EXPLODE,this.x,this.y);
				die();
			}
		}
		
		public function get blood():int
		{
			return _blood;
		}

		public function get exp_rad():int
		{
			return _exp_rad;
		}

		public function set exp_rad(value:int):void
		{
			_exp_rad = value;
		}

		/**
		 *伤害值 
		 */
		public function get hurt():int
		{
			return _hurt;
		}

		/**
		 * @private
		 */
		public function set hurt(value:int):void
		{
			_hurt = value;
		}

		public function get energy():int
		{
			return _energy;
		}

		public function set energy(value:int):void
		{
			_energy = value;
		}

		
	}
}